/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.sun.lwuit.Form;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;

/**
 *
 * @author Kvest
 */
public class FunctionMenu extends Form implements ActionListener
{
    public static final int KIEV_TYPE = 1;
    public static final int DONETSK_TYPE = 2;
    public static final int KIROVOGRAD_TYPE = 3;
    public static final int CHERNOVTSI_TYPE = 4;

    public static final int OK_ID = 1;
    public static final int BACK_ID = 2;
    public static final int ADD_WAIT_ID = 3;
    public static final int FEED_TIME_ID = 4;
    public static final int WITH_PASS_ID = 5;
    public static final int CLOSE_ORDER_ID = 6;
    public static final int DISPETCHER_CALL_ID = 7;
    public static final int LATE_ID = 8;
    public static final int NOT_EXIT_ID = 9;
    public static final int CALL_PASS_ID = 10;
    public static final int FEED_PAID_ID = 11;
    public static final int CANCEL_THROUGHT_BREAKING_ID = 12;
    public static final int REFUSE_ORDER_ID = 13;
    public static final int CLIENT_CALL_ID = 14;
    public static final int MESSAGE_ID = 15;
    public static final int CANCEL_ID = 16;
    public static final int STOP_TIMER_ID = 17;
    public static final int TIMER_ID = 18;
    public static final int TIMER_FINISH_ID = 19;
    public static final int CONNECT_TO_CLIENT = 20;
    public static final int DISPETCHER_CALL_FROM_SETTINGS_ID = 21;

    private Command ok;
    private Command back;
    private FormAction listener;
    private int type;

    //Список действий
    private ThumbList actionsList;

    public FunctionMenu(int type,FormAction listener)
    {
        super("Функции");
        this.listener = listener;
        this.type = type;

        init();
    }

    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);

        addCommandListener(this);

        back= new Command("Назад", BACK_ID);
        addCommand(back);

        ok = new Command("Ok", OK_ID);
        addCommand(ok);

        //Создаем и настраиваем список
        actionsList = new ThumbList();
        actionsList.setSelectCommandText("");
        actionsList.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        render.getStyle().setPadding(Component.BOTTOM, 0);
        render.getStyle().setPadding(Component.TOP, 0);
        actionsList.setRenderer(render);
        actionsList.getStyle().setPadding(Component.BOTTOM, 0);
        actionsList.getStyle().setPadding(Component.TOP, 0);
        addComponent(BorderLayout.CENTER,actionsList);

        switch(type)
        {
            case KIEV_TYPE : createKievCommands(); break;
            case DONETSK_TYPE : createDonetskCommands(); break;
            case KIROVOGRAD_TYPE : createKirovogradCommands(); break;
            case CHERNOVTSI_TYPE : createChernovtsiCommands(); break;
        }
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            Command cmd = evt.getCommand();
            if (cmd.getId() == OK_ID)
            {
                CellItem item = (CellItem)actionsList.getSelectedItem();
                if (item != null)
                {
                    listener.commandAction(item.getId());
                }
            }
            else
            {
                listener.commandAction(cmd.getId());
            }
        }

        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }

    private void createKievCommands()
    {
        actionsList.getModel().addItem(new CellItem(CALL_PASS_ID, "На месте"));
        actionsList.getModel().addItem(new CellItem(ADD_WAIT_ID, "Добавить ожидание"));
        actionsList.getModel().addItem(new CellItem(FEED_TIME_ID, "Время подачи"));
        actionsList.getModel().addItem(new CellItem(WITH_PASS_ID, "С пассажиром"));
        actionsList.getModel().addItem(new CellItem(CLOSE_ORDER_ID, "Закрыть заказ"));
        actionsList.getModel().addItem(new CellItem(DISPETCHER_CALL_ID, "Вызов диспетчера")); 
        actionsList.getModel().addItem(new CellItem(LATE_ID, "Опоздание"));
        actionsList.getModel().addItem(new CellItem(NOT_EXIT_ID, "Нет пассажира"));     
    }

    private void createDonetskCommands()
    {
        actionsList.getModel().addItem(new CellItem(CALL_PASS_ID, "На месте"));
        actionsList.getModel().addItem(new CellItem(FEED_TIME_ID, "Время подачи"));
        actionsList.getModel().addItem(new CellItem(NOT_EXIT_ID, "Нет пассажира"));       
        actionsList.getModel().addItem(new CellItem(WITH_PASS_ID, "С пассажиром"));
        actionsList.getModel().addItem(new CellItem(CLOSE_ORDER_ID, "Закрыть заказ"));
        actionsList.getModel().addItem(new CellItem(CLIENT_CALL_ID, "Звонок клиенту"));
        actionsList.getModel().addItem(new CellItem(DISPETCHER_CALL_FROM_SETTINGS_ID, "Звонок диспетчеру"));
        actionsList.getModel().addItem(new CellItem(MESSAGE_ID ,"Сообщения"));
        actionsList.getModel().addItem(new CellItem(CANCEL_ID ,"Отмена"));
    }

    private void createKirovogradCommands()
    {
        actionsList.getModel().addItem(new CellItem(CALL_PASS_ID, "На месте"));
        actionsList.getModel().addItem(new CellItem(ADD_WAIT_ID, "Добавить ожидание"));
        actionsList.getModel().addItem(new CellItem(FEED_TIME_ID, "Время подачи"));
        actionsList.getModel().addItem(new CellItem(WITH_PASS_ID, "С пассажиром"));
        actionsList.getModel().addItem(new CellItem(CLOSE_ORDER_ID, "Закрыть заказ"));
        actionsList.getModel().addItem(new CellItem(DISPETCHER_CALL_ID, "Вызов диспетчера"));
        actionsList.getModel().addItem(new CellItem(CLIENT_CALL_ID, "Звонок клиенту"));
        actionsList.getModel().addItem(new CellItem(CONNECT_TO_CLIENT, "Звонок клиенту (CallBack)"));
        actionsList.getModel().addItem(new CellItem(LATE_ID, "Опоздание"));
        actionsList.getModel().addItem(new CellItem(NOT_EXIT_ID, "Нет пассажира"));       
    }

    private void createChernovtsiCommands()
    {
        actionsList.getModel().addItem(new CellItem(CALL_PASS_ID, "По адресу"));
        //actionsList.getModel().addItem(new CellItem(ADD_WAIT_ID, "Добавить ожидание"));
        //actionsList.getModel().addItem(new CellItem(FEED_TIME_ID, "Время подачи"));
        //actionsList.getModel().addItem(new CellItem(NOT_EXIT_ID, "Нет пассажира"));
        actionsList.getModel().addItem(new CellItem(CONNECT_TO_CLIENT, "Связь с клиентом"));
        actionsList.getModel().addItem(new CellItem(WITH_PASS_ID, "С пассажиром"));
        actionsList.getModel().addItem(new CellItem(CLOSE_ORDER_ID, "Закрыть заказ"));
        actionsList.getModel().addItem(new CellItem(LATE_ID, "Опоздание"));
        actionsList.getModel().addItem(new CellItem(FEED_PAID_ID, "Подача оплачена"));
        //actionsList.getModel().addItem(new CellItem(CANCEL_THROUGHT_BREAKING_ID, "Отмена в связи с поломкой"));
        //actionsList.getModel().addItem(new CellItem(REFUSE_ORDER_ID, "Отказался выполнять заказ"));
        actionsList.getModel().addItem(new CellItem(REFUSE_ORDER_ID, "Отмена заказа"));
        actionsList.getModel().addItem(new CellItem(STOP_TIMER_ID, "Сброс таймера"));
    }

    private void clear()
    {
        while (actionsList.size() > 0)
        {
            actionsList.getModel().removeItem(0);
        }
    }

    public void startTimerEvent()
    {
        //если не Черновцы - ничего не делаем
        if (type != CHERNOVTSI_TYPE)
        {
            return;
        }

        //Чистим меню
        clear();

        //Создаем по умолчанию
        createChernovtsiCommands();
    }

    public void stopTimerEvent()
    {
        //если не Черновцы - ничего не делаем
        if (type != CHERNOVTSI_TYPE)
        {
            return;
        }
        
        //Чистим меню
        clear();

        actionsList.getModel().addItem(new CellItem(CALL_PASS_ID, "По адресу"));
        //actionsList.getModel().addItem(new CellItem(NOT_EXIT_ID, "Нет пассажира"));
        actionsList.getModel().addItem(new CellItem(CONNECT_TO_CLIENT, "Связь с клиентом"));
        actionsList.getModel().addItem(new CellItem(WITH_PASS_ID, "С пассажиром"));
        actionsList.getModel().addItem(new CellItem(CLOSE_ORDER_ID, "Закрыть заказ"));
        actionsList.getModel().addItem(new CellItem(LATE_ID, "Опоздание"));
        actionsList.getModel().addItem(new CellItem(FEED_PAID_ID, "Подача оплачена"));
        actionsList.getModel().addItem(new CellItem(REFUSE_ORDER_ID, "Отмена заказа"));
        actionsList.getModel().addItem(new CellItem(TIMER_ID, "Таймер"));
    }

    public void startTimerByHandEvent()
    {
        for (int i = actionsList.size() - 1; i >= 0; --i)
        {
            if (((CellItem)actionsList.getModel().getItemAt(i)).getId() == TIMER_ID)
            {
                actionsList.getModel().removeItem(i);
            }
        }

        actionsList.getModel().addItem(new CellItem(TIMER_FINISH_ID, "Добавить ожидание"));
        actionsList.getModel().addItem(new CellItem(STOP_TIMER_ID, "Сброс таймера"));  
    }
}
